package com.example.demo.service.admin;

import com.example.demo.entity.Article;
import com.example.demo.mapper.ArticleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleAdminService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 添加文章
     * @param article
     * @return
     */
    public Boolean addArticle(Article article) {
        Boolean flag = true;
        try {
            if (article.getId() == null) {
                article.setReadTime("0");
                article.setCreateAt(LocalDateTime.now());
                articleMapper.save(article);
            } else {
                articleMapper.updateByPrimaryKey(article);
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    public Boolean deleteArticleById(Integer id) {
        Boolean flag = true;
        try {
            articleMapper.deleteArticle(id);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    public Article selectArticleById(Integer id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        return article;
    }

    /**
     * 查询所有文章,根据创建时间排序
     * @return Article
     */
    public List<Article> selectAll() {
        List<Article> list = articleMapper.selectAll();
        return list;
    }

    /**
     * 根据文章类型查询文章
     * @return Article
     * @author yjq
     */
    public List<Article> selectByType(String articleType) {
        return articleMapper.selectByType(articleType);
    }

    /**
     * 根据文章名查询
     * @param articleTitle
     * @return
     */
    public List<Article> selectByArticleTitle(String articleTitle) {
        return articleMapper.selectByArticleTitle(articleTitle);
    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    public Article selectById(Integer id) {
        articleMapper.updateReadTime(id);
        return articleMapper.selectById(id);
    }

    /**
     * @param name
     * @return
     */
    public List<Article> selectByName(String name) {
        return articleMapper.selectByName(name);
    }

    /**
     * 分页
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<Article> getPage(Integer pageNo, Integer pageSize) {
        //TODO 注释掉的部分是另一种写法，最下面return 后面的是JAVA8的写法
        PageHelper.startPage(pageNo, pageSize);
        List<Article> list = articleMapper.selectAll();
        return new PageInfo<>(list);
        // return PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(() -> userMapper.selectAll());
    }

}
